/*
slice切片，一组特殊的数组
WangGang
2018-07-14
 */
package main

import "fmt"

func main() {
	//初始化切片，类型为int，当前长度为3，最大长度为5
	var number = make([]int ,3,5 )
	//初始化空切片
	var null  []int
	//创建带参数的切片
	numbers := []int{0,1,2,3,4,5,6,7,8,9}

	printSlice(number)
	printSlice(null)
	if(null == nil){
		fmt.Println("切片是空的")
	}
	printSlice(numbers)

	fmt.Println("打印子切片从索引0[包含]到索引4[不包含]:",numbers[0:4])

	//追加新元素 10
	numbers = append(numbers,10)
	printSlice(numbers)

	//创建新切片，并复制原切片内容
	number1 := make([]int,len(numbers),cap(numbers)*2)
	//拷贝numbers内容到number1
	copy(number1,numbers)
	printSlice(number1)

	//获取切片中第二个的内容
	fmt.Println("切片内容1为",numbers[1])
	//遍历切片
	for _,nums :=range numbers{
		fmt.Printf("切片内容为:%x\n",nums)
	}
}

//输出切片的当前设置长度，最大长度及切片内容
func printSlice(x []int){
	fmt.Printf("len=%d cap=%d slice=%d\n",len(x),cap(x),x)
}